package readHandle

import (
	"chart.zozoo.net/app/service/usersOnlineService"
	"chart.zozoo.net/app/socket/connManage"
	"github.com/gorilla/websocket"
)

// ReadHandle 读取socket消息入口
func ReadHandle(conn *websocket.Conn) {
	defer conn.Close()

	addr := conn.RemoteAddr().String()
	for {
		mType, msg, err := conn.ReadMessage()
		if err != nil {
			if mType == -1 {
				//处理用户在线情况
				connInfo := connManage.GetConn(addr)
				err := usersOnlineService.Update(connInfo.UserInfo.Uuid, 0, "")
				if err != nil {
					//写入错误日志
				}

				//客户端断开连接
				connManage.RemoveConn(addr)
				return
			}
			return
		}

		//处理消息
		handleMsg(msg, addr)
	}
}
